import 'package:flutter/material.dart';
import 'package:flutter_study_app/app/router/routers.dart';
import 'package:flutter_study_app/util/dialog_util.dart';
import 'package:flutter_study_app/widget/my_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class OnlineLendSearchPage extends StatefulWidget {
  int type;
  String? searchType;
  String? searchName;

  OnlineLendSearchPage({this.type = 0, this.searchType, this.searchName});

  @override
  State<StatefulWidget> createState() => _OnlineLendSearchPage();
}

class _OnlineLendSearchPage extends State<OnlineLendSearchPage> {
  final List<String> _companyList = ['全部', '事业单位', '国有企业'];
  List<String> _clientList = ['全部', 'A类客户', 'B类客户', 'C类客户'];

  TextEditingController _editingController = TextEditingController();
  String _type = "全部";

  _selectType() {
    DialogUtil.bottomListSelectDialog(
        context, widget.type == 0 ? _clientList.length : _companyList.length,
        title: '请选择类型', buildItemText: (index) {
      return widget.type == 0 ? _clientList[index] : _companyList[index];
    }, hasSelectedItem: (index) {
      String item = widget.type == 0 ? _clientList[index] : _companyList[index];
      return item == _type;
    }, onSelectItem: (index) {
      String item = widget.type == 0 ? _clientList[index] : _companyList[index];
      setState(() {
        _type = item;
      });
    });
  }

  @override
  void initState() {
    super.initState();
    _editingController.text = widget.searchName ?? '';
    _type = widget.searchType ?? "全部";
  }

  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: '客户查询',
        showDivider: true,
        rightMenu: InkWell(
          onTap: () {
            _editingController.text = "";
            _type = "全部";
            setState(() {});
          },
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 10.w),
            child: Text(
              '清除',
              style: TextStyle(color: Color(0xFF666666), fontSize: 14.sp),
            ),
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            color: Colors.white,
            height: 45.w,
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child: Row(
              children: [
                Text(
                  widget.type == 0 ? '客户类型' : '企业类型',
                  style: TextStyle(color: Color(0xFF666666), fontSize: 14.sp),
                ),
                Expanded(
                  child: GestureDetector(
                      onTap: () {
                        _selectType();
                      },
                      child: Container(
                        height: 45.w,
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                _type,
                                style: TextStyle(
                                    fontSize: 14.sp, color: Color(0xFF333333)),
                              ),
                              Image.asset(
                                'assets/images/ic_arrow_right.png',
                                width: 16.w,
                                height: 16.w,
                              )
                            ]),
                      )),
                )
              ],
            ),
          ),
          Container(
            height: 1.w,
            color: Color(0xFFEEEEEE),
          ),
          Container(
            color: Colors.white,
            height: 45.w,
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child: Row(
              children: [
                Text(
                  widget.type == 0 ? '客户名称' : '企业名称',
                  style: TextStyle(color: Color(0xFF666666), fontSize: 14.sp),
                ),
                SizedBox(
                  width: 20.w,
                ),
                Expanded(
                    child: TextField(
                  controller: _editingController,
                  textAlign: TextAlign.right,
                  maxLines: 1,
                  style: TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
                  decoration: InputDecoration(
                    hintText: '请输入',
                    hintStyle:
                        TextStyle(fontSize: 14.sp, color: Color(0xFF999999)),
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                  ),
                ))
              ],
            ),
          ),
          Spacer(),
          SafeArea(
            child: Container(
              padding: EdgeInsets.all(12.w),
              width: double.infinity,
              height: 74.w,
              color: Colors.white,
              child: TextButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Color(0xff009868)),
                ),
                onPressed: () {
                  Map result = {
                    "type": _type,
                    "name": _editingController.text.trim()
                  };
                  Routers.goBackWithParams(context, result);
                },
                child: Text(
                  '搜索',
                  style: TextStyle(color: Colors.white, fontSize: 18.sp),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
